import adminRequest from "@/utils/http/adminRequest.js";
import { v4 as uuidv4 } from 'uuid';

// 获取游戏分页数据
export const gamePageListAPI = (params) => {
    return adminRequest.get('/game/admin/page', {
        params
    })
}

// 批量删除
export const gameDeleteAPI = (ids) => {
    return adminRequest.delete('game/admin/', {
        data: ids
    })
}

// 查询游戏
export const gameGetAPI = (id) => {
    return adminRequest.get(`game/admin/${id}`)
}

// 修改游戏
export const gameEditAPI = (game) => {
    return adminRequest.put('game/admin/', game)
}

// 修改游戏价格
export const gamePriceEditAPI = (gamePrice) => {
    return adminRequest.put('game/admin/price', gamePrice)
}

// 获取-通过游戏id获取游戏预览信息
export const gameViewListGetAPI = (gameId) => {
    return adminRequest.get('/game/admin/viewlist', {
        params: {gameId}
    })
}

// 上传游戏图片预览
export const gameViewImageUploadAPI = (gameId, imageFile) => {
    const formData = new FormData();
    formData.append('gameId', gameId);
    formData.append('image', imageFile);
    return adminRequest.post('game/admin/view/image', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 上传游戏视频预览
export const gameViewVideoUploadAPI = (gameId, videoFile) => {
    // 分块上传
    const chunkSize = 5 * 1024 * 1024 // 每次上传5MB
    const totalChunks = Math.ceil(videoFile.size / chunkSize);
    const fileName = `${uuidv4()}.${videoFile.name.split('.').pop()}`; // 使用UUID生成唯一文件名
    for (let i = 0; i < totalChunks; i++) {
        const start = i * chunkSize;
        const end = Math.min(videoFile.size, start + chunkSize);
        const chunk = videoFile.slice(start, end);

        const formData = new FormData();
        formData.append('gameId', gameId); // 当前游戏的ID
        formData.append('chunk', chunk); //当前文件块
        formData.append('index', i); // 当前块的索引，标识这是第几个块。
        formData.append('totalChunks', totalChunks); // 总块数，服务器可以据此判断是否接收完所有块。
        formData.append('fileName', fileName); // 原始文件名，服务器可以用来还原文件名。

        // 最后一次上传，返回
        if (i === totalChunks - 1) {
            return adminRequest.post('game/admin/view/video', formData, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
        }

        adminRequest.post('game/admin/view/video', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    }
}

// 删除游戏预览
export const gameViewDeleteAPI= (ids) => {
    return adminRequest.delete('game/admin/view', {
        data: ids
    })
}

// 修改游戏详细
export const gameDetailEditAPI = (gameDetailInfo, widerPicture, higherPicture, simplePicture, post) => {
    const formData = new FormData();
    formData.append('gameDetailInfo', JSON.stringify(gameDetailInfo));
    // 只有当imageFile存在且是文件类型时，才附加到表单数据
    if (widerPicture && widerPicture instanceof File) {
        formData.append('widerPicture', widerPicture);
    }
    if (higherPicture && higherPicture instanceof File) {
        formData.append('higherPicture', higherPicture);
    }
    if (simplePicture && simplePicture instanceof File) {
        formData.append('simplePicture', simplePicture);
    }
    if (post && post instanceof File) {
        formData.append('post', post);
    }
    return adminRequest.put('game/admin/detail', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 修改游戏配置需求
export const gameRequireEditAPI = (gameRequire) => {
    return adminRequest.put('game/admin/require', gameRequire)
}

// 添加游戏
export const gameAddAPI = (game) => {
    return adminRequest.post('game/admin', game)
}

// 添加游戏价格
export const gamePriceAddAPI = (gamePrice) => {
    return adminRequest.post('game/admin/price', gamePrice)
}

// 添加游戏价格
export const gameDetailAddAPI = (gameDetailInfo, widerPicture, higherPicture, simplePicture, post) => {
    const formData = new FormData();
    formData.append('gameDetailInfo', JSON.stringify(gameDetailInfo));
    // 只有当imageFile存在且是文件类型时，才附加到表单数据
    formData.append('widerPicture', widerPicture);
    formData.append('higherPicture', higherPicture);
    formData.append('simplePicture', simplePicture);
    formData.append('post', post);
    return adminRequest.post('game/admin/detail', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 添加游戏需求
export const gameRequireAddAPI = (gameRequires) => {
    return adminRequest.post('game/admin/require', gameRequires)
}

// 通过publisherid获取游戏数据
export const gameGetByPublisherIdAPI = (publisherId) => {
    return adminRequest.get('game/games/bypublihserid', {
        params: {publisherId}
    })
}
